


package com.ecocrypt.proxy.forwarder.gui;

import com.ecocrypt.proxy.forwarder.ForwardProxy;

/**
 *
 * @author Bryce Alcock
 */
public class NewForwarderDialogPanel extends javax.swing.JPanel {

    /** Creates new form NewForwarderDialogPanel */
    public NewForwarderDialogPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        localPortLabel = new javax.swing.JLabel();
        localPortInput = new javax.swing.JSpinner();
        remotePortLabel = new javax.swing.JLabel();
        remotePortInput = new javax.swing.JSpinner();
        remoteAddressInput = new javax.swing.JTextField();
        remoteAddressLabel = new javax.swing.JLabel();
        addNewForwarder = new javax.swing.JButton();
        cancelForwarderCreateRequest = new javax.swing.JButton();

        localPortLabel.setText("local port");

        localPortInput.setModel(new javax.swing.SpinnerNumberModel(1080, 0, 64556, 1));
        localPortInput.setToolTipText("select a local IP address to bind this Forwader to.\nThe Local IP Address Must not be currently bound to anything.\n");

        remotePortLabel.setText("remote port");

        remotePortInput.setModel(new javax.swing.SpinnerNumberModel(80, 0, 65555, 1));
        remotePortInput.setToolTipText("Select the Remote Port You would like to forward Local Requests to.\n(E.G. If you want to forward to the Apachce website Port 80 would go here, htt://apache.org:80/)\n");

        remoteAddressInput.setText("apache.org");
        remoteAddressInput.setToolTipText("The IP Address, or DNS Name of the remote machine/endpoint you want to forward to.");
        remoteAddressInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remoteAddressInputActionPerformed(evt);
            }
        });

        remoteAddressLabel.setText("Remote Address");

        addNewForwarder.setText("Apply");
        addNewForwarder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewForwarderActionPerformed(evt);
            }
        });

        cancelForwarderCreateRequest.setText("Cancel");
        cancelForwarderCreateRequest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelForwarderCreateRequestActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(remoteAddressLabel)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(localPortLabel)
                            .addGap(104, 104, 104))
                        .addComponent(remotePortLabel, javax.swing.GroupLayout.Alignment.LEADING))
                    .addComponent(addNewForwarder))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cancelForwarderCreateRequest)
                    .addComponent(localPortInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(remotePortInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(remoteAddressInput, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(78, 78, 78))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(localPortLabel)
                    .addComponent(localPortInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(remotePortLabel)
                    .addComponent(remotePortInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(remoteAddressLabel)
                    .addComponent(remoteAddressInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addNewForwarder)
                    .addComponent(cancelForwarderCreateRequest))
                .addContainerGap(21, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addNewForwarderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewForwarderActionPerformed
        int localPort = (Integer)localPortInput.getValue();
        String remoteAddress = (String)remoteAddressInput.getText();
        int remotePort = (Integer)remotePortInput.getValue();

        String forwarderCreationText = localPort+":"+remoteAddress+":"+remotePort;

        System.out.println("Creating Forwarder : " + forwarderCreationText);
        ForwardProxy.addForwarder(forwarderCreationText);
        this.setVisible(false);

        GuiUtils.findParentDialog(this).dispose();
    }//GEN-LAST:event_addNewForwarderActionPerformed

    private void remoteAddressInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remoteAddressInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_remoteAddressInputActionPerformed

    private void cancelForwarderCreateRequestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelForwarderCreateRequestActionPerformed
        System.out.println("Cancel This Button...");
        this.setVisible(false);
    }//GEN-LAST:event_cancelForwarderCreateRequestActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addNewForwarder;
    private javax.swing.JButton cancelForwarderCreateRequest;
    private javax.swing.JSpinner localPortInput;
    private javax.swing.JLabel localPortLabel;
    private javax.swing.JTextField remoteAddressInput;
    private javax.swing.JLabel remoteAddressLabel;
    private javax.swing.JSpinner remotePortInput;
    private javax.swing.JLabel remotePortLabel;
    // End of variables declaration//GEN-END:variables

}
